<?php
/**
 * $Id: d3blogTextSanitizer.class.php 199 2008-04-09 19:12:31Z mikhail.miguel $
 * @author      Takeshi Kuriyama <kuri@keynext.co.jp>
 */
if(! class_exists('d3blogTextSanitizer')) {

require_once XOOPS_ROOT_PATH.'/class/module.textsanitizer.php';

class d3blogTextSanitizer extends MyTextSanitizer
{
    /*
    * Constructor of this class
    *
    * Gets allowed html tags from admin config settings
    * <br> should not be allowed since nl2br will be used
    * when storing data.
    *
    * @access   private
    *
    * @todo Sofar, this does nuttin' ;-)
    */
    function d3blogTextSanitizer()
    {
        parent::MyTextSanitizer();
    }

    /**
     * Access the only instance of this class
     *
     * @return  object
     *
     * @static
     * @staticvar   object
     */
    function &getInstance()
    {
        static $instance;
        if (!isset($instance)) {
            $instance = new d3blogTextSanitizer();
        }
        return $instance;
    }

    function &displayTarea( $text, $html=0, $smiley=1, $xcode=1, $image=1, $br=1 )
    {
        if( empty( $xcode ) ) {
            if( empty( $html ) ) $text = htmlspecialchars( $text, ENT_QUOTES );
            if( ! empty( $br ) ) $text = nl2br( $text );
            return $text;
        } else {
            $text = $this->prepareXcode( $text );
            $text = $this->postCodeDecode( parent::displayTarea( $text, $html, $smiley, 1, $image, $br ), $image );
            return  $text;
        }
    }

    function prepareXcode( $text )
    {
        $patterns = array(
            '#\n?\[code\]\r?\n?#' ,
            '#\n?\[\/code\]\r?\n?#' ,
            '#\n?\[quote\]\r?\n?#' ,
            '#\n?\[\/quote\]\r?\n?#' ,
        ) ;
        $replacements = array(
            '[code]' ,
            '[/code]' ,
            '[quote]' ,
            '[/quote]' ,
        ) ;
        return preg_replace( $patterns, $replacements, $text );
    }

    function postCodeDecode( $text, $image )
    {
        $patterns = array();
        $replacements = array();

        // [siteimg]
        $patterns[] = "/\[siteimg align=(['\"]?)(left|center|right)\\1]([^\"\(\)\?\&'<>]*)\[\/siteimg\]/sU";
        $patterns[] = "/\[siteimg]([^\"\(\)\?\&'<>]*)\[\/siteimg\]/sU";
        if( $image ) {
            $replacements[] = '<img src="'.XOOPS_URL.'/\\3" align="\\2" alt="" />';

            $replacements[] = '<img src="'.XOOPS_URL.'/\\1" alt="" />';
        } else {
            $replacements[] = '<a href"'.XOOPS_URL.'/\\3" rel="nofollow">'.XOOPS_URL.'/\\3</a>';
            $replacements[] = '<a href"'.XOOPS_URL.'/\\1" rel="nofollow">'.XOOPS_URL.'/\\1</a>';
        }

        return preg_replace($patterns, $replacements, $text);
    }


    /**
     * Replace XoopsCodes with their equivalent HTML formatting
     *
     * @param   string  $text
     * @param   bool    $allowimage Allow images in the text?
     *                              On FALSE, uses links to images.
     * @return  string
     **/
    function xoopsCodeDecode(&$text, $allowimage=1)
    {
        $imgCallbackPattern = "/\[img( align=\w+)]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $text = preg_replace_callback($imgCallbackPattern, array($this, '_filterImgUrl'), $text);

        $patterns = array();
        $replacements = array();
        // RMV: added new markup for intrasite url (allows easier site moves)
        // TODO: automatically convert other URLs to this format if XOOPS_URL matches??
        $patterns[] = "/\[siteurl=(['\"]?)([^\"'<>]*)\\1](.*)\[\/siteurl\]/sU";
        $replacements[] = '<a href="'.XOOPS_URL.'/\\2">\\3</a>';    // without target
        $patterns[] = "/\[url=(['\"]?)(http[s]?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
        $replacements[] = '<a href="\\2">\\3</a>';
        $patterns[] = "/\[url=(['\"]?)(ftp?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
        $replacements[] = '<a href="\\2">\\3</a>';
        $patterns[] = "/\[url=(['\"]?)([^\"'<>]*)\\1](.*)\[\/url\]/sU";
        $replacements[] = '<a href="http://\\2">\\3</a>';
        $patterns[] = "/\[color=(['\"]?)([a-zA-Z0-9]*)\\1](.*)\[\/color\]/sU";
        $replacements[] = '<span style="color: #\\2;">\\3</span>';
        $patterns[] = "/\[size=(['\"]?)([a-z0-9-]*)\\1](.*)\[\/size\]/sU";
        $replacements[] = '<span style="font-size: \\2;">\\3</span>';
        $patterns[] = "/\[font=(['\"]?)([^;<>\*\(\)\"']*)\\1](.*)\[\/font\]/sU";
        $replacements[] = '<span style="font-family: \\2;">\\3</span>';
        $patterns[] = "/\[email]([^;<>\*\(\)\"']*)\[\/email\]/sU";
        $replacements[] = '<a href="mailto:\\1">\\1</a>';
        $patterns[] = "/\[b](.*)\[\/b\]/sU";
        $replacements[] = '<strong style="font-weight:bold">\\1</strong>';  // strong
        $patterns[] = "/\[i](.*)\[\/i\]/sU";
        $replacements[] = '<span style="font-style:italic">\\1</span>'; // i -> span
        $patterns[] = "/\[u](.*)\[\/u\]/sU";
        $replacements[] = '<span style="text_decoration:underline">\\1</span>'; // u -> span
        $patterns[] = "/\[em](.*)\[\/em\]/sU";
        $replacements[] = '<span style="color:#ff0000">\\1</span>';
        $patterns[] = "/\[em=(['\"]?)([a-zA-Z0-9]*)\\1](.*)\[\/em\]/sU";
        $replacements[] = '<span style="color:#\\2;">\\3</span>';
        $patterns[] = "/\[d](.*)\[\/d\]/sU";
        $replacements[] = '<del>\\1</del>';
        //$patterns[] = "/\[li](.*)\[\/li\]/sU";
        //$replacements[] = '<li>\\1</li>';
        $patterns[] = "/\[img align=(['\"]?)(left|center|right)\\1]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img align=(['\"]?)(left|right)\\1 alt=([^\"\(\)\?\&'<>]*)]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img align=(['\"]?)(center)\\1 alt=([^\"\(\)\?\&'<>]*)]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img alt=([^\"\(\)\?\&'<>]*)]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img align=(['\"]?)(left|right)\\1 id=(['\"]?)([0-9]*)\\3]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img align=(['\"]?)(center)\\1 id=(['\"]?)([0-9]*)\\3]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img id=(['\"]?)([0-9]*)\\1]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        if ($allowimage != 1) {
            $replacements[] = '<a href="\\3">\\3</a>';
            $replacements[] = '<a href="\\1">\\1</a>';
            $replacements[] = '<a href="\\4" title="\\3">\\4</a>';
            $replacements[] = '<a href="\\4" title="\\3">\\4</a>';
            $replacements[] = '<a href="\\2" title="\\1">\\2</a>';
            $replacements[] = '<a href="'.XOOPS_URL.'/image.php?id=\\4">\\5</a>';
            $replacements[] = '<a href="'.XOOPS_URL.'/image.php?id=\\4">\\5</a>';
            $replacements[] = '<a href="'.XOOPS_URL.'/image.php?id=\\2">\\3</a>';
        } else {
            $replacements[] = '<img src="\\3" class="blog\\2" alt="" />';
            $replacements[] = '<img src="\\1" alt="" />';
            $replacements[] = '<img src="\\4" class="blog\\2" alt="\\3" />';
            $replacements[] = '<span class="blog\\2"><img src="\\4" alt="\\3" /></span>';
            $replacements[] = '<img src="\\2" alt="\\1" />';
            $replacements[] = '<img src="'.XOOPS_URL.'/image.php?id=\\4" class="blog\\2" alt="\\5" />';
            $replacements[] = '<span class="blog\\2"><img src="'.XOOPS_URL.'/image.php?id=\\4" alt="\\5" /></span>';
            $replacements[] = '<img src="'.XOOPS_URL.'/image.php?id=\\2" alt="\\3" />';
        }
        $patterns[] = "/\[quote]/sU";
        $replacements[] = '<br />'._QUOTEC.'<span class="blogQuote">';   // div -> span(display block)
        $patterns[] = "/\[\/quote]/sU";
        $replacements[] = '</span>';    // /div -> /span
        $patterns[] = "/\[blockquote]/sU";
        $replacements[] = '<span class="blogQuote">';
        $patterns[] = "/\[\/blockquote]/sU";
        $replacements[] = '</span>';
        $patterns[] = "/javascript:/si";
        $replacements[] = "java script:";
        $patterns[] = "/about:/si";
        $replacements[] = "about :";
        return preg_replace($patterns, $replacements, $text);
    }

    function codeConv($text, $xcode = 1, $image = 1){
        if($xcode != 0){
            $patterns = "/\[code](.*)\[\/code\]/esU";
            if ($image != 0) {
                // image allowed
                $replacements = "'<pre class=\"blogCode\"><code>'.d3blogTextSanitizer::codeSanitizer('$1').'</code></pre>'";
                //$text =& $this->xoopsCodeDecode($text);
            } else {
                // image not allowed
                $replacements = "'<pre class=\"blogCode\"><code>'.d3blogTextSanitizer::codeSanitizer('$1', 0).'</code></pre>'";
                //$text =& $this->xoopsCodeDecode($text, 0);
            }
            $text =  preg_replace($patterns, $replacements, $text);
        }
        return $text;
    }

    function codeSanitizer($str, $image = 1){
        if($image != 0){
            $str = $this->xoopsCodeDecode(
                $this->htmlSpecialChars(str_replace('\"', '"', base64_decode($str)))
                );
        }else{
            $str = $this->xoopsCodeDecode(
                $this->htmlSpecialChars(str_replace('\"', '"', base64_decode($str))),0
                );
        }
        return $str;
    }
}

}
?>